﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TreePartData
{
    /// <summary>
    /// 树这部分的类型
    /// </summary>
    public TreePartType Type;
    /// <summary>
    /// 树所处的位置
    /// </summary>
    public GridData GridPos;
    /// <summary>
    /// 该部分的根节点
    /// </summary>
    public TreePartData Root;
    /// <summary>
    /// 该节点的延伸节点
    /// </summary>
    public List<TreePartData> Sons = new List<TreePartData>();

    public static TreePartData CreateTreePart(GridData gridPos, TreePartData root)
    {
        var type = GetTreePartType(gridPos, root);
        if (type == TreePartType.Node)
        {
            return new TreeNodeData(gridPos, root);
        }
        else
        {
            return new TreePartData(type, gridPos, root);
        }
    }

    public void AddSon(TreePartData son)
    {
        if (Sons.Count < 2)
        {
            Sons.Add(son);
        }
    }
    public GrowUpRes CollectRes()
    {
        GrowUpRes result = new GrowUpRes();
        if(this is TreeNodeData && (this as TreeNodeData).IsLeaf)
        {
            result.SunNum += Const.AirOrginValue + (Const.DirtStar - GridPos.PosY - 1) * Const.AirAddValue;
        }
        if(Type == TreePartType.Root)
        {
            result.ShitNum += EnumConvert.GetGridTypeValue(GridPos.Type);
            GridPos.Type = EnumConvert.GetLowerDirt(GridPos.Type);
        }
        return result;
    }
    protected TreePartData(TreePartType type, GridData gridPos, TreePartData root)
    {
        Type = type;
        Root = root;
        GridPos = gridPos;
        gridPos.TreePart = this;
    }

    private static TreePartType GetTreePartType(GridData gridPos, TreePartData root)
    {
        TreePartType type = global::TreePartType.Seed;
        if (root != null)
        {
            if (root.Type == global::TreePartType.Seed)
            {
                type = gridPos.PosY >= root.GridPos.PosY ? global::TreePartType.Root : global::TreePartType.Node;
            }
            else
            {
                type = root.Type;
            }
        }
        return type;
    }

    public static GrowUpRes GetGrowUpResCost(GridData grid, TreePartData part)
    {
        var type = GetTreePartType(grid, part);
        GrowUpRes result = new GrowUpRes();
        switch (type)
        {
            case TreePartType.Root:
                result.SunNum += Const.RootGrowCost;
                break;
            case TreePartType.Node:
                if (part is TreeNodeData)
                {
                    result.ShitNum += ((part as TreeNodeData).Length - 1)*Const.NodeAddCost + Const.NodeOrginGrowUpCost;
                }
                else
                {
                    result.ShitNum += Const.NodeOrginGrowUpCost;
                }
                break;
            case TreePartType.Seed:
            default:
                throw new ArgumentOutOfRangeException();
        }
        return result;
    }
}
